﻿// For an introduction to the Blank template, see the following documentation:
// http://go.microsoft.com/fwlink/?LinkId=232509
(function () {
    "use strict";

    var app = WinJS.Application;
    var activation = Windows.ApplicationModel.Activation;
    function log(text) {
        logDiv.innerHTML += text + "<br/>";
    }
    app.onactivated = function (args) {
        if (args.detail.kind === activation.ActivationKind.launch) {
            if (args.detail.previousExecutionState !== activation.ApplicationExecutionState.terminated) {
                // TODO: This application has been newly launched. Initialize
                // your application here.
            } else {
                // TODO: This application has been reactivated from suspension.
                // Restore application state here.
            }
            args.setPromise(WinJS.UI.processAll());

            xhrTest.onclick = function (e) {
                var time = new Date().getTime();
                var data = '<?xml version="1.0" encoding="utf-8"?>' +
                            '<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" ' +
                            ' xmlns:xsd="http://www.w3.org/2001/XMLSchema" ' +
                            ' xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">' +
                            '  <soap:Body>' +
                            '    <ChangeLengthUnit xmlns="http://www.webserviceX.NET/">' +
                            '      <LengthValue>' + mmValue.value + '</LengthValue>' +
                            '      <fromLengthUnit>Centimeters</fromLengthUnit>' +
                            '      <toLengthUnit>Inches</toLengthUnit>' +
                            '    </ChangeLengthUnit>' +
                            '  </soap:Body>' +
                            '</soap:Envelope>';

                var options = {
                    url: "http://www.webservicex.net/length.asmx",
                    type: "post",
                    headers: {
                        "Content-Type": "text/xml; charset=utf-8",
                        "SOAPAction": "http://www.webserviceX.NET/ChangeLengthUnit"
                    },
                    data: data
                };

                WinJS.xhr(options)
                .done(
                    function (request) {
                        var doc = request.responseXML.documentElement;
                        var output = doc.getElementsByTagName("ChangeLengthUnitResult");

                        log("xhrTest ellapsed " + ((new Date().getTime()) - time).toString() + "ms");
                        result.innerHTML = "La longitud es " + window.toStaticHTML(output[0].textContent) + " pulgadas";
                        result.style.backgroundColor = "#00A000";
                    },
                    function (error) {
                        result.innerHTML = "Status: " + error.status + " : " + error.statusText;
                        result.style.backgroundColor = "#FF0000";
                    },
                    function (progress) {
                        result.innerText = "Ready state is " + progress.readyState;
                        result.style.backgroundColor = "#0000A0";
                    });
            };

            csTest.onclick = function (e) {
                var time = new Date().getTime();
                var cl = new SoapClient.LengthUnits();
                cl.getWithProgressAsync(mmValue.value, SoapClient.LengthUnitService.Lengths.centimeters, SoapClient.LengthUnitService.Lengths.inches).done(
                    function (c) {
                        log("csTest ellapsed " + ((new Date().getTime()) - time).toString() + "ms");
                        result.innerHTML = "La longitud es " + c + " pulgadas";
                        result.style.backgroundColor = "#00a000";
                    },
                    function (err) {
                        result.innerHTML = window.toStaticHTML(err);
                        result.style.backgroundColor = "#FF0000";
                    },
                    function (progress) {
                        result.innerHTML = window.toStaticHTML("state: " + progress);
                        result.style.backgroundColor = "#0000a0";
                    }
                    )
            }
            
        }
    };

    app.oncheckpoint = function (args) {
        // TODO: This application is about to be suspended. Save any state
        // that needs to persist across suspensions here. You might use the
        // WinJS.Application.sessionState object, which is automatically
        // saved and restored across suspension. If you need to complete an
        // asynchronous operation before your application is suspended, call
        // args.setPromise().
    };

    app.start();
})();